package com.education.platform.api.controller.user;

import com.education.platform.api.config.ThreadUser;
import com.education.platform.api.service.OrderReceiptService;
import com.education.platform.api.service.ReceiptAddressService;
import com.education.platform.api.vo.request.UserOrderConfirmPayVO;
import com.education.platform.api.vo.request.UserOrderUpdateVO;
import com.education.platform.api.vo.response.*;
import com.education.platform.common.exception.BusinessException;
import com.education.platform.dao.entity.OrderReceiptDO;
import com.education.platform.dao.entity.ReceiptAddressDO;
import com.education.platform.dao.entity.UserOrderDO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.education.platform.common.util.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import org.springframework.beans.BeanUtils;
import com.education.platform.api.service.UserOrderService;
import org.springframework.validation.annotation.Validated;
import com.education.platform.api.vo.request.UserOrderQueryVO;

/**
* describe: 订单
* @author : tomato
* @date: 2020-06-14 09:47:11.640
**/
@RestController
@Api("订单")
@RequestMapping("userOrder")
@Validated
public class UserOrderController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(UserOrderController.class);
    
	@Autowired
    private UserOrderService userOrderService;

    @Autowired
    private OrderReceiptService orderReceiptService;

    @Autowired
    private ReceiptAddressService receiptAddressService;



    /**
     * 提交订单
     * @param updateVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "提交订单")
    @PostMapping("submitOrder")
    public OrderCreateResponseVO submitOrder(@RequestBody @Valid UserOrderUpdateVO updateVO) {
        UserOrderDO userOrderDTO = userOrderService.createOrder(updateVO);
        OrderCreateResponseVO responseVO = BeanUtil.convert(userOrderDTO,OrderCreateResponseVO.class);
        return  responseVO;
    }

    /**
     * 确认支付
     * @param
     * @return 分页参数
     */
    @ApiOperation(value = "确认支付")
    @PostMapping("confirmPay")
    public boolean confirmPay(@RequestBody @Valid UserOrderConfirmPayVO updateVO) {
        return userOrderService.confirmPay(updateVO.getOrderId());
    }



    /**
     * 订单列表
     * @param userOrderQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询订单")
    @PostMapping("getPageList")
    public UserOrderPageInfoVO selectPageList(@RequestBody @Valid UserOrderQueryVO userOrderQueryVO) {
        UserOrderDO userOrder = BeanUtil.convert(userOrderQueryVO, UserOrderDO.class);
        Page<UserOrderResponseVO> page = PageHelper.startPage(userOrderQueryVO.getPageNum(), userOrderQueryVO.getPageSize());
        userOrder.setUserId(ThreadUser.getUserId());
        userOrder.setDeleted(false);
//        List<UserOrderDO> userOrderList = userOrderService.getList(userOrder);
        List<UserOrderDO> userOrderList = userOrderService.getAreadyPayOrderList(userOrder);
        UserOrderPageInfoVO userOrderPageInfo = new UserOrderPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), userOrderPageInfo);
        List<UserOrderResponseVO> voList = BeanUtil.convert(userOrderList, UserOrderResponseVO.class);
        userOrderPageInfo.setList(voList);

        return userOrderPageInfo;
    }


    /**
     * 查询
     * @param orderNo 订单号
     * @return
     */
    @ApiOperation(value = "订单详情")
    @GetMapping("details")
    public OrderDetailResponseVO details(@RequestParam String orderNo) {
        String info = String.format("The method name[details] params:%s",orderNo);
        LOGGER.info(info);
        OrderReceiptDO orderReceipt = new OrderReceiptDO();
        orderReceipt.setOrderNo(orderNo);
        OrderReceiptDO receipt = orderReceiptService.get(orderReceipt);

        UserOrderDO orderQuery = new UserOrderDO();
        orderQuery.setOrderNo(orderNo);
        UserOrderDO order = userOrderService.get(orderQuery);
        if(order ==null){
            throw new BusinessException("订单不存在");
        }
        OrderDetailResponseVO responseVO =  BeanUtil.convert(order, OrderDetailResponseVO.class);

        if(receipt != null){
            responseVO.setReceiptName(receipt.getReceiptName());
            responseVO.setReceiptPhone(receipt.getReceiptPhone());
            responseVO.setDetailAddress(receipt.getDetailAddress());
        }
        return responseVO;

    }
    /**
     * 获取默认收货地址
     * @param
     * @return
     */
    @ApiOperation(value = "获取默认收货地址")
    @GetMapping("getDefaultAddress")
    public ReceiptAddressResponseVO getDefaultAddress(){
        //收货信息
        ReceiptAddressDO addressDO = receiptAddressService.getByUserId(ThreadUser.getUserId());
        if(addressDO == null){
            return new ReceiptAddressResponseVO();
        }
        return BeanUtil.convert(addressDO,ReceiptAddressResponseVO.class);
    }

}
